# from pyecharts.charts import Page, WordCloud
from pyecharts import options as opts
from pyecharts.charts import Page, ThemeRiver
import re
#1
# key1 = ['董卓', '刘备', '吕布', '曹操','关羽','张飞','何进','韩忠','李肃','陈宫','公孙瓒','袁绍','张角']
# openfile = open('C:/Users/chenrujie/Desktop/实验 七、八/三国演义.txt', encoding='utf-8')
# readtxt = openfile.read()
#
# chapter6_start = "第六回　焚金阙董卓行凶　匿玉玺孙坚背约"
# charper1to5 = readtxt[:readtxt.find(chapter6_start)]
# WordCloud().add("",
#         [ (name, charper1to5.count(name)) for name in key1]
#         ).render()

#2

names = ['董卓', '刘备', '吕布', '曹操', '关羽', '张飞', '袁绍']
openfile = open('C:/Users/chenrujie/Desktop/实验 七、八/三国演义.txt', encoding='utf-8')
readtxt = openfile.read()

chapter20_start = "第二十一回　曹操煮酒论英雄　关公赚城斩车胄"
charper20 = readtxt[:readtxt.find(chapter20_start)]

chapters = re.split("第.{1,3}回", charper20)[1:]  # 切分了章节
id_chapters = enumerate(chapters)

data = []
for x, chapter in id_chapters:
        data.extend([["{}".format(x + 1), chapter.count(name), name] for name in names])
print(data)
c = (
        ThemeRiver()
                .add(
                names,
                eval(repr(data)),
                singleaxis_opts=opts.SingleAxisOpts(type_="value", pos_bottom="10%"),
        )
)

c.render()